package com.bing.bingPro.util;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.bing.bingPro.controller.vo.zRankVo;
import com.bing.bingPro.controller.vo.zTrendVo;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Service
public class TNetWorkExportResult {

    @SuppressWarnings("unchecked")
    public void ponExport(HttpServletResponse response, List<Map<String, Object>> resultList, String year, String filename) throws IOException {
        ExcelWriter writer = ExcelUtil.getBigWriter();
        writer.renameSheet(0, resultList.get(0).get("name").toString().replace("rank","占比趋势图"));
        List<Map<String,Object>> excelMapList = Lists.newArrayList();
        writer.setSheet(resultList.get(0).get("name").toString().replace("rank","占比趋势图"));
        List<zTrendVo> dataList = (List<zTrendVo>) resultList.get(0).get("data");
        if (year.length() !=4) {
            for (zTrendVo map : dataList) {
                Map<String, Object> exclMap = new LinkedHashMap<>();
                exclMap.put("日期", map.getTime());
                exclMap.put("超限占比", Objects.isNull(map.getPercentRate()) ? null : new BigDecimal(map.getPercentRate().toString())
                        .multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP) + "%");
                excelMapList.add(exclMap);
            }
        }else{
            for (zTrendVo map : dataList) {
                Map<String, Object> exclMap = new LinkedHashMap<>();
                exclMap.put("日期", map.getTime());
                exclMap.put("日均超限占比", Objects.isNull(map.getOverRunRate()) ? null : new BigDecimal(map.getOverRunRate().toString())
                        .multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP) + "%");
                exclMap.put("累计超限占比", Objects.isNull(map.getPercentRate()) ? null : new BigDecimal(map.getPercentRate().toString())
                        .multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP) + "%");
                excelMapList.add(exclMap);
            }
        }
        writer.write(excelMapList, true);
        writer.autoSizeColumnAll(); // 自动设置列宽，

        if (year.length() !=4) {
            writer.setSheet(resultList.get(0).get("name").toString().replace("rank","数量趋势图"));
            List<Map<String, Object>> excel1MapList = new ArrayList<>();
            // 占比和数量用的一个结果集合
            for (zTrendVo map : dataList) {
                Map<String, Object> exclMap = new LinkedHashMap<>();
                exclMap.put("日期", map.getTime());
                exclMap.put("活跃PON口数量", map.getActivePONNum());
                exclMap.put("超限PON口数量", map.getOverrunPONNum());
                exclMap.put("超限PON口占比",Objects.isNull(map.getPercentRate()) ? null : new BigDecimal(map.getPercentRate().toString())
                        .multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP) + "%");
                excel1MapList.add(exclMap);
            }
            writer.write(excel1MapList, true);
            writer.autoSizeColumnAll(); // 自动设置列宽，
        }
        if (resultList.size() == 2) {
            List<Map<String, Object>> excel2MapList = Lists.newArrayList();
            writer.setSheet(resultList.get(1).get("name").toString());
            List<zRankVo> data2List = (List<zRankVo>) resultList.get(1).get("data");

            if (year.length() !=4) {
                for (zRankVo map : data2List) {
                    Map<String, Object> exclMap = new LinkedHashMap<>();
                    exclMap.put("区域", map.getArea());
                    exclMap.put("活跃PON口数量", map.getActivePONNum());
                    exclMap.put("超限PON口数量", map.getOverrunPONNum());
                    exclMap.put("超限占比", Objects.isNull(map.getPercentRate()) ? null : new BigDecimal(map.getPercentRate().toString())
                            .multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP) + "%");
                    excel2MapList.add(exclMap);
                }
            }else{
                for (zRankVo map : data2List) {
                    Map<String, Object> exclMap = new LinkedHashMap<>();
                    exclMap.put("区域", map.getArea());
                    exclMap.put("活跃PON口数量", map.getActivePONNum());
                    exclMap.put("超限PON口数量", map.getOverrunPONNum());
                    exclMap.put("日均超限占比", Objects.isNull(map.getOverRunRate()) ? null : new BigDecimal(map.getOverRunRate().toString())
                            .multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP) + "%");
                    exclMap.put("累计超限占比", Objects.isNull(map.getPercentRate()) ? null : new BigDecimal(map.getPercentRate().toString())
                            .multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP) + "%");
                    excel2MapList.add(exclMap);
                }
            }
            writer.write(excel2MapList, true);
            writer.autoSizeColumnAll(); // 自动设置列宽，
        }
        exportWriter(writer, filename, response);
    }


    private void exportWriter(ExcelWriter writer, String excelName, HttpServletResponse response) throws IOException {
        //response为HttpServletResponse对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(excelName, "UTF-8") + ".xls");
        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }
}
